package com.sikaryofficial.backend.controller.prize;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sikaryofficial.backend.constant.PrizeTypeEnum;
import com.sikaryofficial.backend.domain.dto.SysDictDataDTO;
import com.sikaryofficial.backend.domain.dto.req.prize.PrizeListReq;
import com.sikaryofficial.backend.domain.dto.req.prize.PrizeRedeemListReq;
import com.sikaryofficial.backend.domain.dto.req.prize.PrizeRedeemUpdateReq;
import com.sikaryofficial.backend.domain.dto.req.prize.PrizeReq;
import com.sikaryofficial.backend.domain.dto.resp.prize.PrizeDTO;
import com.sikaryofficial.backend.domain.dto.resp.prize.PrizeRedeemDTO;
import com.sikaryofficial.backend.manager.PrizeManager;
import com.sikaryofficial.common.core.domain.R;
import com.sikaryofficial.common.core.utils.PageUtil;
import com.sikaryofficial.common.core.web.controller.BaseController;
import com.sikaryofficial.common.core.web.domain.PageResult;
import com.sikaryofficial.common.log.annotation.Log;
import com.sikaryofficial.common.log.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 奖品Controller
 *
 * @author qinjinyuan
 * @date 2024-07-12
 */
@RestController
@RequestMapping("/prize/backend")
@Api(tags = "V3.5-礼品后台管理")
@Slf4j
public class PrizeBackendController extends BaseController {
    @Autowired
    private PrizeManager prizeManager;
    /**
     * 后台管理-隐藏活动设置
     */
    @Log(title = "后台管理-隐藏礼品设置", businessType = BusinessType.UPDATE)
    @GetMapping("/hide/{prizeId}/{hide}")
    @ApiOperation("V3.5-后台管理-隐藏礼品设置")
    public R<Boolean> prizeHidden(
            @ApiParam(value = "礼品ID", required = true) @PathVariable("prizeId") Long prizeId,
            @ApiParam(value = "是否隐藏 0-显示 1-隐藏，默认0-显示", required = true) @PathVariable("hide") Integer hide) {
        return R.ok(prizeManager.prizeHidden(prizeId, hide));
    }
    /**
     * 礼品发货更新
     *
     */
    @PostMapping("/redeemListUpdate")
    @ApiOperation("礼品兑换发货状态更新")
    public R<Boolean> prizeRedeemListUpdate(@Validated @RequestBody PrizeRedeemUpdateReq req) {
        return R.ok(prizeManager.prizeRedeemListUpdate(req.getList()));
    }
    /**
     * 后台管理-奖项类型
     */
    @GetMapping("/getPrizeType")
    @ApiOperation("奖项类型")
    public R<List<SysDictDataDTO>> getPrizeType() {
        PrizeTypeEnum[] reportTypeEnums = PrizeTypeEnum.values();
        List<SysDictDataDTO> result = new ArrayList<>();
        for (PrizeTypeEnum reportTypeEnum : reportTypeEnums) {
            SysDictDataDTO sysDictDataDTO = new SysDictDataDTO();
            result.add(sysDictDataDTO);
            sysDictDataDTO.setDictLabel(reportTypeEnum.getName());
            sysDictDataDTO.setDictValue(reportTypeEnum.getCode() + "");
            sysDictDataDTO.setDictType("prize_type");
        }
        return R.ok(result);
    }

    /**
     * 查询礼品分页列表
     */
    @PostMapping("/listPage")
    @ApiOperation("查询礼品分页列表")
    public PageResult<PrizeDTO> listPage(@Validated @RequestBody PrizeListReq req) {
        // 查询获奖列表
        IPage<PrizeDTO> page = prizeManager.listPage(req);
        return PageUtil.success(page.getRecords(), page.getTotal());
    }

    /**
     * 查询礼品分页列表
     */
    @PostMapping("/redeem/listPage")
    @ApiOperation("礼品兑换分页列表")
    public PageResult<PrizeRedeemDTO> redeemListPage(@Validated @RequestBody PrizeRedeemListReq req) {
        // 查询获奖列表
        IPage<PrizeRedeemDTO> page = prizeManager.redeemListPage(req);
        return PageUtil.success(page.getRecords(), page.getTotal());
    }

    /**
     * 奖品详细信息
     */
    @GetMapping(value = "/detail/{prizeId}")
    @ApiOperation("奖品详细信息")
    public R<PrizeDTO> getInfo(@PathVariable("prizeId") Long prizeId) {
        return R.ok(prizeManager.getInfo(prizeId));
    }

    /**
     * 奖品新增或修改
     */
    @Log(title = "奖品", businessType = BusinessType.INSERT)
    @PostMapping(value = "/saveOrUpdate")
    @ApiOperation("奖品新增或修改")
    public R<Long> saveOrUpdate(@RequestBody PrizeReq req) {
        return R.ok(prizeManager.saveOrUpdate(req));
    }

    /**
     * 删除奖品
     */
    @Log(title = "奖品", businessType = BusinessType.DELETE)
    @GetMapping(value = "/remove/{prizeIds}")
    @ApiOperation("删除礼品")
    public R<Boolean> remove(
            @ApiParam(value = "prizeIds，支持一个或多个,英文逗号分隔", required = true)
            @PathVariable String prizeIds) {
        prizeManager.remove(prizeIds);
        return R.ok(Boolean.TRUE);
    }
}
